import tkinter as tk
from utils.weatherAPIUtil import WeatherAPIUtil
from utils.dataUtil import csv2dict

class WeatherApp:
    def __init__(self,root):
        self.root = root
        #设置基本内容
        self.root.title("天气预报")
        self.root.geometry("500x500")
        self.creat_data()
        self.creat_ui()

    def creat_data(self):
        self.citys=csv2dict()
        self.w=WeatherAPIUtil()

    def creat_ui(self):
        lable_name=[
            "城市",
            "降水",
            "温度",
            "气压",
            "风向",
            "风向角",
            "风速",
            "风力范围",
            "体感",
        ]
        self.string_list=[]
        for idx,name in enumerate(lable_name):
            name_label=tk.Label(self.root,text=name,padx=10,pady=10)
            name_label.grid(row=idx,column=0)
            string_label=tk.Label(self.root,text="",padx=10,pady=10)
            string_label.grid(row=idx,column=1)
            self.string_list.append(string_label)

        self.currentString=tk.StringVar(self.root)
        self.currentString.set("北京")
        self.option_menu=tk.OptionMenu(self.root,self.currentString,*self.citys.keys())
        self.option_menu.grid(row=len(lable_name),column=0)
        self.btn=tk.Button(self.root,text="获取",command=self.getData)
        self.btn.grid(row=len(lable_name),column=1)

    def getData(self):
        data=self.w.getWeather(self.citys[self.currentString.get()])
        for item,value in zip(self.string_list,data):
            item['text']=value
